package com.apes.fn.scm.used.model;

import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.fn.scm.used.server.UsedStockService;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

/**
 * 功能：旧件出库单明细
 *
 * @author BieBi
 * @create 2018-9-4 17:01:42
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = "war_used_deliver_item")
public class UsedDeliverItem extends Model {
    public static final String CONST_TABLE_NAME = "war_used_deliver_item";

    @Id
    @Comment("编码")
    @Column(name = "ID")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "旧件出库单明细")
    private String id;

    @Comment("出库单号")
    @NotNull
    @ManyToOne(optional = false)
    @JoinColumn(name = "used_deliver_id", referencedColumnName = "id", nullable=false)
    private UsedDeliver usedDeliver;  // 出库单号

    @Comment("公司")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    private PartyRole company; //公司编码

    @NotNull
    @Comment("旧件")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "used_id", referencedColumnName = "id", nullable=false)
    private Used used;  // 旧件编码



    @Transient
    private String uom;

    public String getUom() {
        return used.getUom();
    }

    @Comment("出库数量")
    @Column(name = "qty_deliver", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    private double qtyDeliver = 0; // 出库数量

    /***
     * 从定价过程带出
     */
    @Column(name = "price_unit", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("单价")
    private double priceUnit = 0;

    /***
     * 手动输入或者计算
     */
    @Column(name = "price_total", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("总计")
    private double priceTotal = 0;


    @Transient
    private double stockQty = 0;

    @Transient
    private double insuranceStockQty = 0;

    public double getInsuranceStockQty() {
        // 非可编辑状态直接返回
        if(!this.usedDeliver.getState().equals("create")) return 0;

        UsedStock usedStock = SpringManager.getBean(UsedStockService.class).getStockQty(this.usedDeliver.getLocation(), this.used);
        if(usedStock != null) {
            this.setStockQty(usedStock.getStockQty());
            return usedStock.getInsuranceStockQty();
        } else {
            this.setStockQty(0);
            return 0;
        }
    }

    /***
     * 合计金额(直接取值或者计算)
     */
    public void computeAmt() {
        if( priceUnit > 0 && qtyDeliver > 0) {
            priceTotal = priceUnit * qtyDeliver;
        }
    }

}